import 'package:cloud_medicine/components/author_show.dart';
import 'package:cloud_medicine/components/util.dart';
import 'package:cloud_medicine/http/user_api.dart';
import 'package:cloud_medicine/model/user_verify_detail.dart';
import 'package:cloud_medicine/pages/image_show.dart';
import 'package:flutter/material.dart';

class UserVerifyDetailPage extends StatefulWidget {
  final int id; // 帖子ID

  UserVerifyDetailPage({Key key, @required this.id}) : super(key: key);

  @override
  _UserVerifyDetailPageState createState() => _UserVerifyDetailPageState();
}

class _UserVerifyDetailPageState extends State<UserVerifyDetailPage> {
  bool _loading = false;
  UserVerifyDetail _userVerifyDetail;

  @override
  void initState() {
    super.initState();
    _init();
  }

  // 初始化，向后端请求数据
  Future<Null> _init() async {
    setState(() {
      _loading = true;
    });
    dynamic res = await getUserVerifyDetail_api({'id': widget.id});
    if (res['code'] == 0) {
      if (mounted) {
        setState(() {
          _userVerifyDetail = UserVerifyDetail.init(res['data']);
        });
      }
    } else {
      errorToast(res['message']);
    }
    if (mounted) {
      setState(() {
        _loading = false;
      });
    }
  }

  // 生成gridView的网格视图图片
  List<Widget> getWidgetList() {
    return _userVerifyDetail.files
        .map((file) => getItemContainer(file))
        .toList();
  }

  // 生成每一张图片的样式
  Widget getItemContainer(String file) {
    return GestureDetector(
      child: Container(
        child: FadeInImage.assetNetwork(
          placeholder: 'images/loading.gif', // 占位图
          image: file,
        ),
        color: Colors.grey[100],
      ),
      onTap: () {
        Navigator.of(context).push(MaterialPageRoute(
            builder: (context) => ImageShow(
                  url: file,
                )));
      },
    );
  }

  // 提交审核结果
  _submitVerifyResult(bool isAgree) async {
    print(isAgree);
    dynamic res =
        await submitUserVerifyResult_api({'id': widget.id, 'isAgree': isAgree});
    res['code'] == 0
        ? successToast(res['message'])
        : errorToast(res['message']);
    Navigator.pop(context);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('验证信息'),
        centerTitle: true,
      ),
      body: _loading
          ? loadingWidget(_loading)
          : ListView(
              children: <Widget>[
                Container(
                  padding: EdgeInsets.all(8),
                  child: ListView(
                    shrinkWrap: true, // 为true可以解决子控件必须设置高度的问题
                    physics: NeverScrollableScrollPhysics(), // 禁用滑动事件
                    children: <Widget>[
                      AuthorShow(
                        userID: _userVerifyDetail.authorBasicInfo.id,
                        avatar: _userVerifyDetail.authorBasicInfo.avatar,
                        realname: _userVerifyDetail.authorBasicInfo.realname,
                        createTime: _userVerifyDetail.createTime,
                      ),
                      SizedBox(height: 10),
                      SelectableText(_userVerifyDetail.content),
                      SizedBox(height: 10),
                      GridView.count(
                        physics: new NeverScrollableScrollPhysics(),
                        shrinkWrap: true,
                        crossAxisSpacing: 10.0,
                        mainAxisSpacing: 10.0,
                        crossAxisCount: 3,
                        // 一行显示个数
                        childAspectRatio: 1.0,
                        children: getWidgetList(),
                      ),
                      SizedBox(height: 10),
                    ],
                  ),
                ),
                SizedBox(height: 20),
                Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: <Widget>[
                    RaisedButton(
                      onPressed: () {
                        _submitVerifyResult(true);
                      },
                      color: Colors.green,
                      textColor: Colors.white,
                      child: Text('通过验证'),
                    ),
                    RaisedButton(
                      onPressed: () {
                        _submitVerifyResult(false);
                      },
                      color: Colors.red,
                      textColor: Colors.white,
                      child: Text('不通过验证'),
                    ),
                  ],
                ),
              ],
            ),
    );
  }
}
